//package com.jiangsong.batch;
//
//
//import com.jiangsong.entity.Product;
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.item.database.JpaItemWriter;
//import org.springframework.batch.item.database.JpaPagingItemReader;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.persistence.EntityManagerFactory;
//
//@Configuration
////@EnableBatchProcessing
//public class BatchConfig1 {
//    private final JobBuilderFactory jobBuilderFactory;
//    private final StepBuilderFactory stepBuilderFactory;
//
//    @Autowired
//    private ProductChunkListener productChunkListener;
//
//
//    public BatchConfig1(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
//        this.jobBuilderFactory = jobBuilderFactory;
//        this.stepBuilderFactory = stepBuilderFactory;
//    }
//
//    @Bean
//    public JpaPagingItemReader<Product> reader(EntityManagerFactory entityManagerFactory) {
//        JpaPagingItemReader<Product> reader = new JpaPagingItemReader<>();
//        reader.setEntityManagerFactory(entityManagerFactory);
//        reader.setQueryString("select p from Product p");
//        try {
//            reader.afterPropertiesSet();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return reader;
//    }
//
//    @Bean
//    public JpaItemWriter<Product> writer(EntityManagerFactory entityManagerFactory) {
//        JpaItemWriter<Product> writer = new JpaItemWriter<>();
//        writer.setEntityManagerFactory(entityManagerFactory);
//        return writer;
//    }
//
//    @Bean
//    public Step step(JpaPagingItemReader<Product> reader, ProductItemProcessor processor, JpaItemWriter<Product> writer) {
//        return stepBuilderFactory.get("step")
//                .<Product, Product>chunk(50)
//                .reader(reader)
//                .processor(new ProductItemProcessor())
//                .writer(writer)
//                .listener(productChunkListener)
//                .build();
//    }
//
//    @Bean
//    public Job job(Step step1) {
//        return jobBuilderFactory.get("job")
//                .start(step1)
//                .build();
//    }
//}